Array vs List¶lst = [1, 2.5, "hi", [1,2,3], True]
lst
[1, 2.5, 'hi', [1, 2, 3], True]
id(lst)
2056599975552
x = 5
id(x)
140733608203176
y = 5
id(y)
140733608203176
# !pip install numpy
import numpy as np
lst = [1, 2, 3, 4, 5]
arr = np.array(lst)
arr
array([1, 2, 3, 4, 5])
arr.dtype
dtype('int32')
arr = arr.astype('float32')
arr
array([1., 2., 3., 4., 5.], dtype=float32)
arr
array([1, 2, 3, 4, 5])
np.array(lst)
arr = np.array(lst, dtype='float32')
arr
array([1., 2., 3., 4., 5.], dtype=float32)
for i in lst:
print(i)
1 2 3 4 5
for i in arr:
print(i)
1.0 2.0 3.0 4.0 5.0
arr = np.array([1,2,3,4,5])
arr
array([1, 2, 3, 4, 5])
arr.ndim
1
arr.shape
(5,)
arr = np.array([[1],[2],[3],[4],[5]])
arr.shape
(5, 1)
arr = np.array([
[1, 2, 3],
[1, 2, 3],
[1, 2, 3],
[1, 2, 3],
[1, 2, 3],
[1, 2, 3],
])
arr.shape
(6, 3)
lst = [7, 2, 9, 10, 11]
arr = np.array(lst)
print(arr.shape)
arr
(5,)
array([ 7, 2, 9, 10, 11])
lst = [[7, 2, 9, 10, 11]]
arr = np.array(lst)
print(arr.shape)
arr
(1, 5)
array([[ 7, 2, 9, 10, 11]])
lst = [[[5, 3, 4], [9, 1, 3]], [[5, 3, 4], [9, 1, 3]], [[5, 3, 4], [9, 1, 3]]]
arr = np.array(lst)
print(arr.shape)
arr
(3, 2, 3)
array([[[5, 3, 4],
[9, 1, 3]],
[[5, 3, 4],
[9, 1, 3]],
[[5, 3, 4],
[9, 1, 3]]])
lst = [1, 2, 3, 4, 5]
lst
[1, 2, 3, 4, 5]
lst.append(6)
lst
[1, 2, 3, 4, 5, 6]
arr = np.array([1, 2, 3, 4, 5])
arr
array([1, 2, 3, 4, 5])
np.append(arr, [6])
array([1, 2, 3, 4, 5, 6])
arr2 = np.arange(0, 9)
arr2 = arr2.reshape((3,3))
arr2
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
np.append(arr2, [6]) # It flattens then append
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 6])
np.max(arr)
5
arr.max()
5
lst1 = [1, 2, 3]
lst2 = [4, 5, 6]
lst1.extend(lst2)
lst1
[1, 2, 3, 4, 5, 6]
lst1 = [1, 2, 3]
lst2 = [4, 5, 6]
lst1 + lst2
[1, 2, 3, 4, 5, 6]
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr1 + arr2
array([5, 7, 9])
# np.array(range(6))
array([0, 1, 2, 3, 4, 5])
arr = np.arange(6)
print(arr)
arr.shape
[0 1 2 3 4 5]
(6,)
arr.reshape((2, 3))
array([[0, 1, 2],
[3, 4, 5]])
arr.reshape((3, 2))
array([[0, 1],
[2, 3],
[4, 5]])
arr.reshape((4, 1))
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[72], line 1 ----> 1 arr.reshape((4, 1)) ValueError: cannot reshape array of size 6 into shape (4,1)
arr.reshape((6, 1))
array([[0],
[1],
[2],
[3],
[4],
[5]])
arr.reshape((6))
array([0, 1, 2, 3, 4, 5])
arr #.shape
array([0, 1, 2, 3, 4, 5])
arr.reshape((3, -1))
array([[0, 1],
[2, 3],
[4, 5]])
arr.reshape((-1))
array([0, 1, 2, 3, 4, 5])
arr.reshape((3, -1)) # 3, 2
array([[0, 1],
[2, 3],
[4, 5]])
arr.reshape((6, -1)) # 6, 1
array([[0],
[1],
[2],
[3],
[4],
[5]])
arr.reshape((-1)) # 6, 1
array([0, 1, 2, 3, 4, 5])
lst1 = [1, 2, 3, 4, 5]
arr1 = np.array(lst1)
arr1
array([1, 2, 3, 4, 5])
lst2 = [10, 20, 30, 40, 50]
arr2 = np.array(lst2)
arr2
array([10, 20, 30, 40, 50])
arr1 + arr2
array([11, 22, 33, 44, 55])
arr1 * arr2
array([ 10, 40, 90, 160, 250])
arr1 * 10
array([10, 20, 30, 40, 50])
arr1 / 10
array([0.1, 0.2, 0.3, 0.4, 0.5])
arr1**2
array([ 1, 4, 9, 16, 25])
np.power(arr1, 2)
array([ 1, 4, 9, 16, 25], dtype=int32)
np.sqrt(arr1)
array([1. , 1.41421356, 1.73205081, 2. , 2.23606798])
arr1**0.5
array([1. , 1.41421356, 1.73205081, 2. , 2.23606798])
np.rad2deg(3.14)
179.9087476710785
np.pi
3.141592653589793
np.rad2deg(np.pi)
180.0
arr = np.arange(24).reshape(2, -1, 4)
arr
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
arr.shape
(2, 3, 4)
len(arr.shape)
3
arr.size
24
arr.dtype
dtype('int32')
arr.astype('float')
array([[[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.]],
[[12., 13., 14., 15.],
[16., 17., 18., 19.],
[20., 21., 22., 23.]]])
arr = np.arange(24).reshape(3, 8)
arr
array([[ 0, 1, 2, 3, 4, 5, 6, 7],
[ 8, 9, 10, 11, 12, 13, 14, 15],
[16, 17, 18, 19, 20, 21, 22, 23]])
arr.reshape(-1)
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23])
arr.ravel()
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23])
arr.flatten()
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23])
arr
array([[ 0, 1, 2, 3, 4, 5, 6, 7],
[ 8, 9, 10, 11, 12, 13, 14, 15],
[16, 17, 18, 19, 20, 21, 22, 23]])
arr3 = arr.reshape((1, 3, 1, 8, 1, 1))
arr3
array([[[[[[ 0]],
[[ 1]],
[[ 2]],
[[ 3]],
[[ 4]],
[[ 5]],
[[ 6]],
[[ 7]]]],
[[[[ 8]],
[[ 9]],
[[10]],
[[11]],
[[12]],
[[13]],
[[14]],
[[15]]]],
[[[[16]],
[[17]],
[[18]],
[[19]],
[[20]],
[[21]],
[[22]],
[[23]]]]]])
arr3.ravel()
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23])
arr3.reshape(-1)
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23])
img = np.zeros((256, 256, 3))
img[:128, :128, 0] = 1.0
# img[128:, 128:, 1] = 1.0
import matplotlib.pyplot as plt
plt.imshow(img);
img.shape
(256, 256, 3)
img = np.zeros((64, 256, 256, 3))
img.shape
(64, 256, 256, 3)
6400
img = np.zeros((256, 256, 3))
img.shape
(256, 256, 3)
img.reshape((1, 256, 256, 3)).shape
(1, 256, 256, 3)
np.expand_dims(img, axis=0).shape
(1, 256, 256, 3)
img = np.expand_dims(img, axis=0)
img.shape
(1, 256, 256, 3)
img.reshape((1, -1)).shape
(1, 196608)
img = np.random.random((256, 256, 3))
plt.imshow(img);
np.arange(24).reshape(4, 6)
array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23]])
arr_zeros = np.zeros((3, 4))
arr_zeros
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
np.ones((3,4))
array([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
arr = np.ones([3, 4]) * 50
arr
array([[50., 50., 50., 50.],
[50., 50., 50., 50.],
[50., 50., 50., 50.]])
arr.shape
(3, 4)
arr2 = np.zeros(arr.shape)
arr2
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
arr2 = np.zeros_like(arr)
arr2
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
np.ones_like(arr_zeros)
array([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
np.eye(5)
array([[1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 1.]])
np.linspace(0, 1, 11)
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])
np.arange(0, 1.0000001, 0.1)
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])
# Return random floats in the half-open interval [0.0, 1.0)
np.random.random()
0.7039177137285235
np.random.random((3,4))
array([[0.16910139, 0.79887745, 0.67021357, 0.70816144],
[0.61535757, 0.18376302, 0.40791498, 0.18348666],
[0.32516788, 0.35982663, 0.82460407, 0.00337169]])
np.random.rand(3, 4)
array([[0.27235092, 0.42353828, 0.68604708, 0.61573022],
[0.20079542, 0.12818271, 0.08706981, 0.49023664],
[0.62600469, 0.57818759, 0.27746641, 0.17027698]])
# from `low` (inclusive) to `high` (exclusive).
np.random.randint(0, 10, size=(2, 3))
array([[5, 2, 5],
[1, 9, 8]])
# Return a sample (or samples) from the "standard normal" distribution.
np.random.randn(3)
array([-1.59977171, 1.40310673, 0.36754629])
np.random.seed(5)
np.random.rand()
0.22199317108973948
arr = np.random.random((10, 3)) * 10
arr
array([[3.48331994e+00, 5.91222311e+00, 7.75013130e+00],
[6.24754467e+00, 1.59680933e+00, 8.93762653e+00],
[7.30591266e+00, 4.97768019e+00, 6.43640219e+00],
[5.09475094e+00, 1.68208158e+00, 6.41464773e+00],
[9.88167763e+00, 7.70329363e+00, 1.81481209e+00],
[1.19769021e+00, 9.30341843e+00, 9.10097433e+00],
[7.49184660e+00, 7.60531540e+00, 1.42875798e+00],
[8.11944192e+00, 1.92279296e+00, 1.54745110e+00],
[4.07078957e+00, 3.82894367e-03, 8.45517816e+00],
[1.61132453e+00, 7.39250735e+00, 5.82910816e+00]])
arr.mean()
5.621313595430986
arr.mean(axis=0)
array([5.54498867, 5.19038766, 6.12856445])
arr.mean(axis=1)
array([6.1282999 , 8.23874849, 3.87078739, 3.08419968, 6.00425269,
5.03635079, 7.59133363, 4.3727355 , 8.32647883, 3.55994906])
arr.std(axis=1)
array([1.74747934, 3.03229793, 0.96058921, 1.99403616, 3.40740845,
3.7742654 , 2.88528413, 3.01349563, 3.45105999, 2.44167498])
arr.std(axis=0)
array([2.71531091, 3.09567141, 2.92306853])
arr.std(axis=0) ** 2
array([7.37291334, 9.58318147, 8.54432961])
arr.var(axis=0)
array([7.37291334, 9.58318147, 8.54432961])
arr.max(axis=0)
array([9.88167763, 9.30341843, 9.10097433])
arr.argmax(axis=0)
array([4, 5, 5], dtype=int64)
arr = np.arange(12).reshape(3, 4)
arr
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
arr[0][0]
0
arr[0, 0]
0
arr[1, 1]
5
arr[2, 3]
11
arr[(1,2), (1,3)]
array([ 5, 11])
arr[(1,2), (1,3)] = 1000
arr
array([[ 0, 1, 2, 3],
[ 4, 1000, 6, 7],
[ 8, 9, 10, 1000]])
arr
array([[ 0, 1, 2, 3],
[ 4, 1000, 6, 7],
[ 8, 9, 10, 1000]])
arr[:, :]
array([[ 0, 1, 2, 3],
[ 4, 1000, 6, 7],
[ 8, 9, 10, 1000]])
arr[0]
array([0, 1, 2, 3])
arr[0, :]
array([0, 1, 2, 3])
arr[:2, :2]
array([[ 0, 1],
[ 4, 1000]])
arr[1:3, 1:3]
array([[1000, 6],
[ 9, 10]])
arr[1]
array([ 4, 1000, 6, 7])
arr[::]
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
arr
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
arr[::2]
array([ 0, 2, 4, 6, 8, 10, 12])
arr[1::2]
array([ 1, 3, 5, 7, 9, 11])
arr[::-1]
array([12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
arr[::-2]
array([12, 10, 8, 6, 4, 2, 0])
arr = np.random.randint(0, 15, size=12)
arr
array([ 2, 7, 12, 13, 6, 13, 5, 14, 10, 14, 2, 6])
bool_list = []
for i in arr:
if i % 2 == 0:
bool_list.append(i)
bool_list
[2, 12, 6, 14, 10, 14, 2, 6]
arr%2
array([0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0], dtype=int32)
mask = arr%2 == 0
mask
array([ True, False, True, False, True, False, False, True, True,
True, True, True])
arr
array([ 2, 7, 12, 13, 6, 13, 5, 14, 10, 14, 2, 6])
arr[mask]
array([ 2, 12, 6, 14, 10, 14, 2, 6])
mask.astype(int)
array([1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1])
mask.sum()
8
len(arr[mask])
8
mask.dtype
dtype('bool')
arr + 5
array([ 7, 12, 17, 18, 11, 18, 10, 19, 15, 19, 7, 11])

mat_1 = np.array(range(10, 14))
mat_1 = np.reshape(mat_1, (2, 2))
mat_1
array([[10, 11],
[12, 13]])
mat_1 = np.array([[10, 11],
[12, 13]])
mat_1
array([[10, 11],
[12, 13]])
mat_2 = np.array([[30, 31],
[32, 33]])
mat_2
array([[30, 31],
[32, 33]])
mat_1 + mat_2
array([[40, 42],
[44, 46]])
mat_1 - mat_2
array([[-20, -20],
[-20, -20]])
mat_1 * mat_2
array([[300, 341],
[384, 429]])
mat_1 / mat_2
array([[0.33333333, 0.35483871],
[0.375 , 0.39393939]])
mat_1 % mat_2
array([[10, 11],
[12, 13]])

5 * mat_1
array([[50, 55],
[60, 65]])
mat_1 = np.array([[2, 4], [7, 8], [3, 2]])
mat_1 # 3 x 2
array([[2, 4],
[7, 8],
[3, 2]])
mat_2 = np.array([[1, 3], [4, 7]])
mat_2 # 2 x 2
array([[1, 3],
[4, 7]])

np.dot(mat_1, mat_2)
array([[18, 34],
[39, 77],
[11, 23]])
mat_1.dot(mat_2)
array([[18, 34],
[39, 77],
[11, 23]])
mat_1 @ mat_2
array([[18, 34],
[39, 77],
[11, 23]])
np.matmul(mat_1, mat_2)
array([[18, 34],
[39, 77],
[11, 23]])
# np.max(arr)
# arr.max()

mat_1.T
array([[2, 7, 3],
[4, 8, 2]])
np.transpose(mat_1)
array([[2, 7, 3],
[4, 8, 2]])
mat_1.transpose()
array([[2, 7, 3],
[4, 8, 2]])
x = np.linspace(-3, 3, 5)
y = np.linspace(-3, 3, 5)
xx, yy = np.meshgrid(x, y)
plt.scatter(xx, yy)

x = np.linspace(-3, 3, 5)
y = np.linspace(-3, 3, 5)
print(x)
print(y)
[-3. -1.5 0. 1.5 3. ] [-3. -1.5 0. 1.5 3. ]
import matplotlib.pyplot as plt
plt.scatter(x, y);
xx, yy = np.meshgrid(x, y)
print(xx)
[[-3. -1.5 0. 1.5 3. ] [-3. -1.5 0. 1.5 3. ] [-3. -1.5 0. 1.5 3. ] [-3. -1.5 0. 1.5 3. ] [-3. -1.5 0. 1.5 3. ]]
print(yy)
[[-3. -3. -3. -3. -3. ] [-1.5 -1.5 -1.5 -1.5 -1.5] [ 0. 0. 0. 0. 0. ] [ 1.5 1.5 1.5 1.5 1.5] [ 3. 3. 3. 3. 3. ]]
plt.scatter(xx, yy);
import numpy as np
import matplotlib.pyplot as plt
# Generate x and y values using np.linspace
x = np.linspace(-3, 3, 5) # (5,)
y = np.linspace(-3, 3, 5) # (5,)
# Expand dimensions of x and y
x_expanded = np.expand_dims(x, axis=1) # Expands x to shape (5, 1)
y_expanded = np.expand_dims(y, axis=0) # Expands y to shape (1, 5)
# Now, x_expanded and y_expanded are 2D arrays that can be broadcasted
X, Y = x_expanded, y_expanded
# # Flatten the 2D arrays for scatter plotting
# X = X.ravel()
# Y = Y.ravel()
# Scatter plot
plt.scatter(X, Y)
<matplotlib.collections.PathCollection at 0x1de80a16c50>

arr_1 = np.array([[0, 2], [7, 6]])
arr_2 = np.array([[3, 7], [1, 4]])
np.vstack([arr_1, arr_2])
array([[0, 2],
[7, 6],
[3, 7],
[1, 4]])
np.hstack([arr_1, arr_2])
array([[0, 2, 3, 7],
[7, 6, 1, 4]])
Depth stacking
img = np.dstack([arr1, arr2, arr3])

a = np.array([1, 2])
b = np.array([3, 4])
np.dot(a, b)
11
a @ b
11
np.sum(a * b)
11
result = 0
for i in range(len(a)):
result += a[i] * b[i]
result
11
a = np.array([[1, 2]])
b = np.array([[3, 4]])
print(a.shape)
print(b.shape)
(1, 2) (1, 2)
np.dot(a, b)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[506], line 1 ----> 1 np.dot(a, b) File <__array_function__ internals>:200, in dot(*args, **kwargs) ValueError: shapes (1,2) and (1,2) not aligned: 2 (dim 1) != 1 (dim 0)
a @ b.T
array([[11]])
a
array([[1, 2]])
b.T
array([[3],
[4]])
# Magnitude
a = np.array([3, 4])
np.linalg.norm(a)
5.0
import time
t1 = time.time()
for i in range(1000_000):
x= 5+5
t2 = time.time()
t2 - t1
0.05953621864318848
%%time
for i in range(1000_000):
x= 5+5
CPU times: total: 78.1 ms Wall time: 70.1 ms
%%timeit
for i in range(1000_000):
x= 5+5
24.3 ms ± 475 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
def slow_dot_product(a, b):
result = 0
for i in range(len(a)):
result += a[i] * b[i]
return result
a = np.random.randn(1000_000)
b = np.random.randn(1000_000)
slow_dot_product(a, b)
1220.0138861196572
# number of experiments
T = 1000
a = np.random.randn(10_000)
b = np.random.randn(10_000)
tic = time.process_time()
for t in range(T):
slow_dot_product(a, b)
toc = time.process_time()
dt1 = toc - tic
print("Time in sec:", dt1)
Time in sec: 2.75
tic = time.process_time()
for t in range(T):
a @ b
toc = time.process_time()
dt1 = toc - tic
print("Time in sec:", dt1)
Time in sec: 0.015625
2.75 / 0.015625
176.0
arr = np.array([[1, 2], [3, 4]])
arr
array([[1, 2],
[3, 4]])
np.linalg.det(arr)
-2.0000000000000004
np.trace(arr)
5
np.linalg.eig(arr)
(array([-0.37228132, 5.37228132]),
array([[-0.82456484, -0.41597356],
[ 0.56576746, -0.90937671]]))
A = np.array([[1, 1], [1.5, 4]])
b = np.array([2200, 5050])
A_inv = np.linalg.inv(A)
A_inv.dot(b)
array([1500., 700.])
np.linalg.solve(A, b)
array([1500., 700.])
data = np.random.rand(3, 5)
np.corrcoef(data.T)
array([[ 1. , 0.16327515, 0.99999866, 0.90101648, 0.90102699],
[ 0.16327515, 1. , 0.16489153, -0.28085011, 0.5750575 ],
[ 0.99999866, 0.16489153, 1. , 0.90030448, 0.90173654],
[ 0.90101648, -0.28085011, 0.90030448, 1. , 0.62368033],
[ 0.90102699, 0.5750575 , 0.90173654, 0.62368033, 1. ]])
View vs deep-Copy¶arr1 = np.random.randn(2, 3)
arr2 = arr1
# Referenece Equality
print(id(arr1))
print(id(arr2))
arr2 is arr1
2057035845520 2057035845520
True
arr1
array([[-0.14389912, -0.26846048, -0.52502538],
[ 0.91390907, 0.26495336, 1.46164214]])
arr1[0, 0] = 20000
arr1
array([[ 2.00000000e+04, -2.68460478e-01, -5.25025381e-01],
[ 9.13909070e-01, 2.64953357e-01, 1.46164214e+00]])
arr2
array([[ 2.00000000e+04, -2.68460478e-01, -5.25025381e-01],
[ 9.13909070e-01, 2.64953357e-01, 1.46164214e+00]])
arr_view = arr1.view().reshape(-1)
arr_view
array([ 2.00000000e+04, -2.68460478e-01, -5.25025381e-01, 9.13909070e-01,
2.64953357e-01, 1.46164214e+00])
arr_view[0] = 5555555
arr1
array([[ 5.55555500e+06, -2.68460478e-01, -5.25025381e-01],
[ 9.13909070e-01, 2.64953357e-01, 1.46164214e+00]])
arr2
array([[ 5.55555500e+06, -2.68460478e-01, -5.25025381e-01],
[ 9.13909070e-01, 2.64953357e-01, 1.46164214e+00]])
arr2 = 5
arr1
array([[ 5.55555500e+06, -2.68460478e-01, -5.25025381e-01],
[ 9.13909070e-01, 2.64953357e-01, 1.46164214e+00]])
arr2 = np.array([0,7,8,9,5])
arr1
array([[ 5.55555500e+06, -2.68460478e-01, -5.25025381e-01],
[ 9.13909070e-01, 2.64953357e-01, 1.46164214e+00]])
arr_deep = np.copy(arr1)
arr_deep
array([[ 5.55555500e+06, -2.68460478e-01, -5.25025381e-01],
[ 9.13909070e-01, 2.64953357e-01, 1.46164214e+00]])
arr_deep[0] = 0
arr_deep
array([[0. , 0. , 0. ],
[0.91390907, 0.26495336, 1.46164214]])
arr1
array([[ 5.55555500e+06, -2.68460478e-01, -5.25025381e-01],
[ 9.13909070e-01, 2.64953357e-01, 1.46164214e+00]])
!cat simple_dataset_to_read.csv
0|1|2|3|4|5|6|7|8|9|10 1|2|3|4|5|6|7|8|9|10|11 2|3|4|5|6|7|8|9|10|11|12 3|4|5|6|7|8|9|10|11|12|13 4|5|6|7|8|9|10|11|12|13|14 5|6|7|8|9|10|11|12|13|14|15 6|7|8|9|10|11|12|13|14|15|16 7|8|9|10|11|12|13|14|15|16|17 8|9|10|11|12|13|14|15|16|17|18 9|10|11|12|13|14|15|16|17|18|19 10|11|12|13|14|15|16|17|18|19|20
arr = np.genfromtxt("simple_dataset_to_read.csv", delimiter='|')
arr
array([[ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.],
[ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11.],
[ 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.],
[ 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13.],
[ 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14.],
[ 5., 6., 7., 8., 9., 10., 11., 12., 13., 14., 15.],
[ 6., 7., 8., 9., 10., 11., 12., 13., 14., 15., 16.],
[ 7., 8., 9., 10., 11., 12., 13., 14., 15., 16., 17.],
[ 8., 9., 10., 11., 12., 13., 14., 15., 16., 17., 18.],
[ 9., 10., 11., 12., 13., 14., 15., 16., 17., 18., 19.],
[10., 11., 12., 13., 14., 15., 16., 17., 18., 19., 20.]])
new_arr = arr / 50
new_arr
array([[0. , 0.02, 0.04, 0.06, 0.08, 0.1 , 0.12, 0.14, 0.16, 0.18, 0.2 ],
[0.02, 0.04, 0.06, 0.08, 0.1 , 0.12, 0.14, 0.16, 0.18, 0.2 , 0.22],
[0.04, 0.06, 0.08, 0.1 , 0.12, 0.14, 0.16, 0.18, 0.2 , 0.22, 0.24],
[0.06, 0.08, 0.1 , 0.12, 0.14, 0.16, 0.18, 0.2 , 0.22, 0.24, 0.26],
[0.08, 0.1 , 0.12, 0.14, 0.16, 0.18, 0.2 , 0.22, 0.24, 0.26, 0.28],
[0.1 , 0.12, 0.14, 0.16, 0.18, 0.2 , 0.22, 0.24, 0.26, 0.28, 0.3 ],
[0.12, 0.14, 0.16, 0.18, 0.2 , 0.22, 0.24, 0.26, 0.28, 0.3 , 0.32],
[0.14, 0.16, 0.18, 0.2 , 0.22, 0.24, 0.26, 0.28, 0.3 , 0.32, 0.34],
[0.16, 0.18, 0.2 , 0.22, 0.24, 0.26, 0.28, 0.3 , 0.32, 0.34, 0.36],
[0.18, 0.2 , 0.22, 0.24, 0.26, 0.28, 0.3 , 0.32, 0.34, 0.36, 0.38],
[0.2 , 0.22, 0.24, 0.26, 0.28, 0.3 , 0.32, 0.34, 0.36, 0.38, 0.4 ]])
np.savetxt('test_save.csv', new_arr, delimiter=',', fmt='%0.2e')
!cat test_save.csv
0.00e+00,2.00e-02,4.00e-02,6.00e-02,8.00e-02,1.00e-01,1.20e-01,1.40e-01,1.60e-01,1.80e-01,2.00e-01 2.00e-02,4.00e-02,6.00e-02,8.00e-02,1.00e-01,1.20e-01,1.40e-01,1.60e-01,1.80e-01,2.00e-01,2.20e-01 4.00e-02,6.00e-02,8.00e-02,1.00e-01,1.20e-01,1.40e-01,1.60e-01,1.80e-01,2.00e-01,2.20e-01,2.40e-01 6.00e-02,8.00e-02,1.00e-01,1.20e-01,1.40e-01,1.60e-01,1.80e-01,2.00e-01,2.20e-01,2.40e-01,2.60e-01 8.00e-02,1.00e-01,1.20e-01,1.40e-01,1.60e-01,1.80e-01,2.00e-01,2.20e-01,2.40e-01,2.60e-01,2.80e-01 1.00e-01,1.20e-01,1.40e-01,1.60e-01,1.80e-01,2.00e-01,2.20e-01,2.40e-01,2.60e-01,2.80e-01,3.00e-01 1.20e-01,1.40e-01,1.60e-01,1.80e-01,2.00e-01,2.20e-01,2.40e-01,2.60e-01,2.80e-01,3.00e-01,3.20e-01 1.40e-01,1.60e-01,1.80e-01,2.00e-01,2.20e-01,2.40e-01,2.60e-01,2.80e-01,3.00e-01,3.20e-01,3.40e-01 1.60e-01,1.80e-01,2.00e-01,2.20e-01,2.40e-01,2.60e-01,2.80e-01,3.00e-01,3.20e-01,3.40e-01,3.60e-01 1.80e-01,2.00e-01,2.20e-01,2.40e-01,2.60e-01,2.80e-01,3.00e-01,3.20e-01,3.40e-01,3.60e-01,3.80e-01 2.00e-01,2.20e-01,2.40e-01,2.60e-01,2.80e-01,3.00e-01,3.20e-01,3.40e-01,3.60e-01,3.80e-01,4.00e-01
# pip install scipy
from scipy import linalg
# Creating input array
A = np.array([[1, 2, -3], [2, -5, 4], [5, 4, -1]])
# Solution Array
b = np.array([[-3], [13], [5]])
# Solve the linear algebra
x = linalg.solve(A, b)
x
array([[ 2.],
[-1.],
[ 1.]])
np.linalg.norm(A.dot(x) - b )
4.440892098500626e-16
(1, 5) + (2, 5)
a = np.array([1, 2, 3, 4, 5]) # (5,)
b = np.array([[0, 0, 0, 0, 0], [5, 5, 5, 5, 5]]) # (2, 5)
print(a.shape)
print(b.shape)
a + b
(5,) (2, 5)
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10]])
z = a + b
z.shape
(2, 5)
a = np.array([[1, 2, 3, 4, 5]]) # (1, 5)
b = np.array([[0, 0, 0, 0, 0], [5, 5, 5, 5, 5]]) # (2, 5)
print(a.shape)
print(b.shape)
a + b
(1, 5) (2, 5)
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10]])
a = np.array([[1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]) # (2, 5)
b = np.array([[0, 0, 0, 0, 0], [5, 5, 5, 5, 5]]) # (2, 5)
print(a.shape)
print(b.shape)
a + b
(2, 5) (2, 5)
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10]])
a = np.array([1, 2, 3, 4, 5]) # (5,)
b = np.array([[0, 5], [0, 5], [0, 5], [0, 5], [0, 5]]) # (5, 2)
print(a.shape)
print(b.shape)
a + b
(5,) (5, 2)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[589], line 7 4 print(a.shape) 5 print(b.shape) ----> 7 a + b ValueError: operands could not be broadcast together with shapes (5,) (5,2)
a = np.array([[1, 2, 3, 4, 5]]) # (1, 5)
b = np.array([[0, 5], [0, 5], [0, 5], [0, 5], [0, 5]]) # (5, 2)
print(a.shape)
print(b.shape)
a + b
(1, 5) (5, 2)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[591], line 7 4 print(a.shape) 5 print(b.shape) ----> 7 a + b ValueError: operands could not be broadcast together with shapes (1,5) (5,2)
a = np.array([[1, 2, 3, 4, 5]]) # (1, 5)
b = np.array([[0, 0, 0, 0, 0], [5, 5, 5, 5, 5]]) # (2, 5)
print(a.shape)
print(b.shape)
a + b
(1, 5) (2, 5)
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10]])
a = np.array([[1], [2], [3], [4], [5]]) # (5, 5)
b = np.array([[5, 5, 5, 5, 5]]) # (5, 5)
print(a.shape)
print(b.shape)
a + b
(5, 1) (1, 5)
array([[ 6, 6, 6, 6, 6],
[ 7, 7, 7, 7, 7],
[ 8, 8, 8, 8, 8],
[ 9, 9, 9, 9, 9],
[10, 10, 10, 10, 10]])
Exercise: Implement the numpy vectorized version of the L1 loss. You may find the function abs(x) (absolute value of x) useful.
Reminder:
Expected Output:
| L1 | 1.1 |
def L1(yhat, y):
"""
Arguments:
yhat -- vector of size m (predicted labels)
y -- vector of size m (true labels)
Returns:
loss -- the value of the L1 loss function defined above
"""
### START CODE HERE ### (≈ 1 line of code)
loss = None
### END CODE HERE ###
return loss
yhat = np.array([.9, 0.2, 0.1, .4, .9])
y = np.array([1 , 0 , 0 , 1, 1])
l1_loss = L1(yhat,y)
print("L1 = " + str(l1_loss))
Exercise: Implement the numpy vectorized version of the L2 loss. There are several way of implementing the L2 loss but you may find the function np.dot() useful. As a reminder, if $x = [x_1, x_2, ..., x_n]$, then np.dot(x,x) = $\sum_{j=0}^n x_j^{2}$.
Expected Output:
| L2 | 0.43 |
def L2(yhat, y):
"""
Arguments:
yhat -- vector of size m (predicted labels)
y -- vector of size m (true labels)
Returns:
loss -- the value of the L2 loss function defined above
"""
### START CODE HERE ### (≈ 1 line of code)
loss = None
### END CODE HERE ###
return loss
yhat = np.array([.9, 0.2, 0.1, .4, .9])
y = np.array([1 , 0 , 0 , 1, 1])
l2_loss = L2(yhat,y)
print("L2 = " + str(l2_loss))